﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using System.Threading;
using System.IO;
using System.Reflection;

namespace BSC_OTC_THONGBAO
{
    public partial class BSC_OTC_THONGBAO_Services : ServiceBase
    {
        #region khai bao bien
        private Code.CL_Ultility _cl = new Code.CL_Ultility();
        ProcessDB _ProcessDB = new ProcessDB();
        System.Timers.Timer timer = new System.Timers.Timer();
        string loaiEmail = ConfigurationManager.AppSettings["LoaiEmail"].ToString();
        #endregion

        public BSC_OTC_THONGBAO_Services()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // ghi log
            Thread.Sleep(10000);
            timer.Interval = 1000 * 10; // 60 seconds  
            timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
            timer.Enabled = true;
        }

        public void test()
        {
            if(CheckDieuKien())
            {
                // Lấy danh sách các công việc cần cảnh báo 
                DataTable congviecs = this._ProcessDB.GetAllJobInToday();
                if (congviecs.Rows.Count>0)
                {
                    foreach (DataRow row in congviecs.Rows)
                    {
                        string soHD = Convert.ToString(row["sohd"]);
                        string tenHD = Convert.ToString(row["tenhd"]);
                        string chitietCV = Convert.ToString(row["chitietcv"]);
                        string tgCanhBao = Convert.ToDateTime(row["tgcanhbao"]).ToShortDateString();
                        string tgthuchien = Convert.ToDateTime(row["tgthuchien"]).ToShortDateString();
                        string ghichu = soHD + "." + tenHD + "." + chitietCV + "." + tgCanhBao + "." + tgthuchien + ".";
                        try
                        {
                            SendEmail _objSendEmail = new SendEmail(soHD, tenHD, chitietCV, tgCanhBao, tgthuchien);
                            // sau khi gui email , ghi vao bang bsc_emaillog 
                            this._ProcessDB.UpdateToEmailLog(loaiEmail, ghichu);
                        }
                        catch (Exception ex)
                        {
                            
                        }
                    }
                }
            }
        }

        private bool CheckDieuKien()
        {
            // kiem tra xem neu chua den 8h thi se ko gui email
            if (DateTime.Now.Hour < 8)
            {
                return false;
            }

            // kiem tra xem neu da gui email trong ngay hom nay thi ko gui nua
            if (_ProcessDB.CheckSendedEmailToDay() == false)
            {
                return false;
            }

            return true;
        }

        public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
        {
            if (CheckDieuKien())
            {
                // Lấy danh sách các công việc cần cảnh báo 
                DataTable congviecs = this._ProcessDB.GetAllJobInToday();
                if (congviecs.Rows.Count > 0)
                {
                    foreach (DataRow row in congviecs.Rows)
                    {
                        string soHD = Convert.ToString(row["sohd"]);
                        string tenHD = Convert.ToString(row["tenhd"]);
                        string chitietCV = Convert.ToString(row["chitietcv"]);
                        string tgCanhBao = Convert.ToDateTime(row["tgcanhbao"]).ToShortDateString();
                        string tgthuchien = Convert.ToDateTime(row["tgthuchien"]).ToShortDateString();
                        string ghichu = soHD + "." + tenHD + "." + chitietCV + "." + tgCanhBao + "." + tgthuchien + ".";
                        try
                        {
                            SendEmail _objSendEmail = new SendEmail(soHD, tenHD, chitietCV, tgCanhBao, tgthuchien);
                            // sau khi gui email , ghi vao bang bsc_emaillog 
                            this._ProcessDB.UpdateToEmailLog(loaiEmail, ghichu);
                        }
                        catch (Exception ex)
                        {
                            _cl.UPDATE_LOG_FILE("ERR", "", "OnTimer()", "OnTimer()", ex.Message);
                        }
                    }
                }
            }
        }  

        protected override void OnStop()
        {
            timer.Dispose();
        }
    }
}
